package com.j180.controller.detail;

import com.j180.bean.Detail;
import com.j180.bean.ResultBean;
import com.j180.service.detail.IDetailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService iDetailService;

    /**
     * 代分页的所有查询
     * @param curpage 当前页数
     * @param pageSize 每页几条
     * @return
     */
    @GetMapping("/detailsAll/{curpage}/{pageSize}")
    public ResultBean<List<Detail>> detailsAll(@PathVariable int curpage, @PathVariable int pageSize){

        List<Detail> list = iDetailService.showDetailAllInfo(curpage,pageSize);

        ResultBean<List<Detail>> resultBean =  new ResultBean<>();

        if (list!=null){
            resultBean.setCode(1);
            resultBean.setMsg("ok");
            resultBean.setT(list);
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
            resultBean.setMsg(null);
        }

        return resultBean;

    }

    /**
     * 根据id查询
     * @param id 传入要查询的id
     * @return
     */
    @GetMapping("/detailid/{id}")
    public ResultBean<Detail> DetailByIdInfo(@PathVariable int id){

        Detail detail = iDetailService.showDetailByIdInfo(id);

        ResultBean<Detail> resultBean = new ResultBean<>();

        if (detail!=null){
            resultBean.setCode(1);
            resultBean.setMsg("ok");
            resultBean.setT(detail);
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
            resultBean.setT(null);

        }

        return resultBean;
    }

    @PostMapping("/add")
    public ResultBean<String> addDetail(Detail detail){


        int num = iDetailService.addDetailInfo(detail);

        ResultBean resultBean = new ResultBean();
        if (num==1){
            resultBean.setCode(1);
            resultBean.setMsg("ok");
            return resultBean;
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
            return resultBean;

        }

    }
    @PostMapping("/update")
    public ResultBean<String> updateDetail(Detail detail){

        int num = iDetailService.updateDetailInfo(detail);

        ResultBean resultBean = new ResultBean();

        if (num==1){
            resultBean.setCode(1);
            resultBean.setMsg("ok");

            return resultBean;
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
            return resultBean;
        }

    }

    @DeleteMapping("/delete/{id}")
    public ResultBean<String> delDetail(@PathVariable int id){

        int num = iDetailService.deleteDetailByIdInfo(id);

        ResultBean resultBean = new ResultBean();

        if (num==1){
            resultBean.setCode(1);
            resultBean.setMsg("ok");

            return resultBean;
        }else {
            resultBean.setCode(0);
            resultBean.setMsg("no");
            return resultBean;
        }

    }
}
